/**
 * Copyright &copy; 2012-2016 <a 

href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights 

reserved.
 */
package com.thinkgem.jeesite.modules.tb_parts.web;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.tb_parts.entity.TbParts;
import com.thinkgem.jeesite.modules.tb_parts.service.TbPartsService;

/**
 * 汽车配件管理Controller
 * @author LiJie
 * @version 2017-06-10
 */
@Controller
@RequestMapping(value = "${adminPath}/tb_parts/tbParts")
public class TbPartsController extends BaseController {

	@Autowired
	private TbPartsService tbPartsService;
	/**
	 * 获取单条数据
	 * @param String
	 * @return TbParts
	 */
	@ModelAttribute
	public TbParts get(@RequestParam(required=false) String id) {
		TbParts entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = tbPartsService.get(id);
		}
		if (entity == null){
			entity = new TbParts();
		}
		return entity;
	}
	/**
	 * 获取单条数据
	 * @param TbParts
	 * @param HttpServletRequest
	 * @param HttpServletResponse
	 * @param Model
	 * @return TbParts
	 */
	@RequiresPermissions("tb_parts:tbParts:view")
	@RequestMapping(value = {"list", ""})
	public String list(TbParts tbParts, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<TbParts> page = tbPartsService.findPage(new Page<TbParts>(request, response), tbParts); 
		model.addAttribute("page", page);
		return "modules/tb_parts/tbPartsList";
	}

	@RequiresPermissions("tb_parts:tbParts:view")
	@RequestMapping(value = "form")
	public String form(TbParts tbParts, Model model) {
		model.addAttribute("tbParts", tbParts);
		return "modules/tb_parts/tbPartsForm";
	}
	/**
	 * 保存/修改条数据
	 * @param TbParts
	 * @param RedirectAttributes
	 * @param Model
	 * @return String
	 */
	@RequiresPermissions("tb_parts:tbParts:edit")
	@RequestMapping(value = "save")
	public String save(TbParts tbParts, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, tbParts)){
			return form(tbParts, model);
		}
		tbPartsService.save(tbParts);
		addMessage(redirectAttributes, "保存汽车配件成功");
		return "redirect:"+Global.getAdminPath()+"/tb_parts/tbParts/?repage";
	}
	/**
	 * 删除配件
	 * @param TbParts
	 * @param RedirectAttributes
	 * @param 
	 */
	@RequiresPermissions("tb_parts:tbParts:edit")
	@RequestMapping(value = "delete")
	public String delete(TbParts tbParts, RedirectAttributes redirectAttributes) {
		tbPartsService.delete(tbParts);
		addMessage(redirectAttributes, "删除汽车配件成功");
		return "redirect:"+Global.getAdminPath()+"/tb_parts/tbParts/?repage";
	}
	/**
	 * 汽车配件详情
	 * @param String
	 * @return ArrayList<TbParts>
	 */
	@RequestMapping("partsDetails")
	@ResponseBody
	public ArrayList<TbParts> partsDetails(TbParts tbParts, 
    HttpServletRequest request, HttpServletResponse response, Model model){
		String id = request.getParameter("id");
		ArrayList<TbParts> TbParts = tbPartsService.partsDetails(id);
		return TbParts;
	}
}
